#region File Description
//-----------------------------------------------------------------------------
// OptionsMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
#endregion

namespace IcyTowerMobile
{
	/// <summary>
	/// The about screen is brought up over the top of the main menu
	/// screen, and gives the user a clue who has developed this game.
	/// </summary>
	class AboutScreen : GameScreen
	{

		private string text;

		/// <summary>
		/// Tracks a fading selection effect on the entry.
		/// </summary>
		/// <remarks>
		/// The entries transition out of the selection effect when they are deselected.
		/// </remarks>
		private float selectionFade;

		private ContentManager content;
		private Texture2D fhLogo;

		/// <summary>
		/// Constructor.
		/// </summary>
		public AboutScreen()
		{
			text = "Developers:\n- Thomas Jaeger\n- Robert Schenkenfelder\n\nNote that we are not FreeLunchDesign\n(www.freelunchdesign.com), the originator of\nIcyTower. We just ported the awesome game\nto Windows Phone.\n\n\nThis game was developed at the University\nof Applied Sciences Upper Austria, Campus\nHagenberg, Mobile Computing Department.";

			TransitionOnTime = TimeSpan.FromSeconds(0.5);
			TransitionOffTime = TimeSpan.FromSeconds(0.5);
		}

		/// <summary>
		/// Load graphics content for the game.
		/// </summary>
		public override void LoadContent()
		{
			if (content == null)
				content = new ContentManager(ScreenManager.Game.Services, "Content");

			// load FH OOE logo
			fhLogo = content.Load<Texture2D>("logo_fhooe");
		}

		/// <summary>
		/// Responds to user input, in this case only cancelling the screen.
		/// </summary>
		public override void HandleInput(InputState input)
		{
			// we cancel the current menu screen if the user presses the back button
			PlayerIndex player;
			if (input.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player))
			{
				OnCancel(player);
			}
		}

		/// <summary>
		/// Handler for when the user has cancelled the menu.
		/// </summary>
		protected virtual void OnCancel(PlayerIndex playerIndex)
		{
			ExitScreen();
		}


		/// <summary>
		/// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
		/// </summary>
		protected void OnCancel(object sender, PlayerIndexEventArgs e)
		{
			OnCancel(e.PlayerIndex);
		}

		public override void Update(GameTime gameTime, bool otherScreenHasFocus,
													   bool coveredByOtherScreen)
		{
			base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

			// When the menu selection changes, entries gradually fade between
			// their selected and deselected appearance, rather than instantly
			// popping to the new state.
			float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;

			selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
		}

		public override void Draw(GameTime gameTime)
		{
			// Make the menu slide into place during transitions, using a
			// power curve to make things look more interesting (this makes
			// the movement slow down as it nears the end).
			float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

			Vector2 position = new Vector2(30f, 330f);

			if (ScreenState == ScreenState.TransitionOn)
				position.X -= transitionOffset * 256;
			else
				position.X += transitionOffset * 512;

			Color color = Color.White;

			// Pulsate the size of the text.
			double time = gameTime.TotalGameTime.TotalSeconds;

			float pulsate = (float)Math.Sin(time * 6) + 1;

			float scale = 1 + pulsate * 0.05f * selectionFade;

			// Modify the alpha to fade text out during transitions.
			color *= TransitionAlpha;

			SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
			SpriteFont font = ScreenManager.AboutFont;

			spriteBatch.Begin();

			Vector2 origin = new Vector2(0, font.LineSpacing / 2);

			// draw about text
			spriteBatch.DrawString(font, text, position, color, 0,
								   origin, scale, SpriteEffects.None, 0);

			// draw FH OOE logo
			position = new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2 - fhLogo.Width / 2, ScreenManager.GraphicsDevice.Viewport.Height - fhLogo.Height - 10);
			if (ScreenState == ScreenState.TransitionOn)
				position.X -= transitionOffset * 256;
			else
				position.X += transitionOffset * 512;

			Rectangle fhRect = new Rectangle((int)position.X, (int)position.Y,
											 fhLogo.Width, fhLogo.Height);
			spriteBatch.Draw(fhLogo, fhRect, Color.White);

			spriteBatch.End();
		}

	}
}
